/**
 * 连接RMQ参数 (推演版)
 */
import { defineStore } from "pinia";
import { ref, computed } from "vue";
import { IRmqFormState } from "@@/typings/header-toolbar";
import { lStorage } from "@@/utils/StorageStore";
import { RabbitMq_Config } from "@/../public/config"

export interface RmqStateTuiyan {
  // ip
  rmqIp: string;
  // 用户名
  rmqUsername: string;
  // 密码
  rmqPassword: string;
  // 虚拟主机
  rmqHost: string;
  // 订阅gameId
  gameId: string;
  // 连接状态
  connectState: boolean;
}

export const rmqStateTuiyan = defineStore("rmq-store-tuiyan", () => {
  const rmqIp = ref(RabbitMq_Config.ip);
  const rmqUsername = ref(RabbitMq_Config.password);
  const rmqPassword = ref(RabbitMq_Config.password);
  const rmqHost = ref(RabbitMq_Config.host);
  let gameId = localStorage.getItem("gameIdTuiyan")
  const connectState = ref(false);
  // rmq链接地址
  const getRmqUrl = computed(() => {
    return "ws://" + rmqIp.value + ":15674/ws";
  });

  // 获取订阅通道
  const getSubscriptions = computed(() => {
    return {
      all: `current.${gameId}.situation`,
    };
  });

  // 设置连接状态
  function setConnectState(params: boolean) {
    connectState.value = params;
  }

  // 更新state
  function setRmqValue(params: IRmqFormState) {
    rmqIp.value = params.rmqIp;
    rmqUsername.value = params.rmqUsername;
    rmqPassword.value = params.rmqPassword;
    gameId = params.gameId;
  }

  return {
    rmqIp,
    rmqUsername,
    rmqPassword,
    rmqHost,
    gameId,
    connectState,
    getRmqUrl,
    getSubscriptions,
    setConnectState,
    setRmqValue
  };
});
